package io;

import java.io.*;
import java.util.Scanner;

public class IODemo14 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的源文件:");
        String srcPath = scanner.nextLine();
        System.out.println("请输入要复制的目标文件:");
        String destPath = scanner.nextLine();

        //合法性判定
        File srcFile = new File(srcPath);
        if(!srcFile.isFile()) {
            System.out.println("源文件路径有误!");
            return;
        }
        //destPath 不要求对应的文件存在,但是目录得存在
        File destFile = new File(destPath);
        if (!destFile.getParentFile().isDirectory()) {
            System.out.println("目标路径有误!");
            return;
        }

        //复制操作
        try(InputStream inputStream = new FileInputStream(srcPath);
            OutputStream outputStream = new FileOutputStream(destPath)) {
            while(true) {
                byte[] buffer = new byte[1024];
                int n = inputStream.read(buffer);
                if(n == -1) {
                    break;
                }
                //把读到的内容写到outputStream中
                outputStream.write(buffer,0,n);
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
